# Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
# easyAda is licensed under Mulan PubL v2.
# You can use this software according to the terms and conditions of the Mulan PubL v2.
# You may obtain a copy of Mulan PubL v2 at:
#          http://license.coscl.org.cn/MulanPubL-2.0
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PubL v2 for more details.

define check_empty
    ifeq ($$($(1)),)
        $$(error "$(1): empty")
    endif
endef

var_list = SOURCE_DIR BINARY_DIR
$(foreach var_name,$(var_list),$(eval $(call check_empty,$(var_name))))

#ifeq ($(CONFIG_DIR),)
    CONFIG_DIR := $(SOURCE_DIR)
#endif
#export CONFIG_DIR

include ${SOURCE_DIR}/scripts/config.mk

.PHONY : all
all: kernel

USER_INCLUDES := -I${SOURCE_DIR}/include/uapi

KERNEL_INCLUDES	:= \
		-I${SOURCE_DIR}/include/plat/${PLAT}  \
		-I${SOURCE_DIR}/include/arch/${ARCH}  \
		-I${SOURCE_DIR}/include  \
		$(USER_INCLUDES)

INCLUDES := $(KERNEL_INCLUDES)

export INCLUDES

kernel : $(BINARY_DIR)
	$(Q) test -f $(CONFIG_DIR)/.config || { echo "configure first"; exit 1; }
	$(Q)$(MAKE) -f ${SOURCE_DIR}/scripts/Makefile.build.mk component=kernel

$(BINARY_DIR):
	$(Q)echo " mkdir     $@"
	$(Q)-mkdir $@

.PHONY : clean
clean:
	$(Q)rm -rf $(BINARY_DIR)/kernel
	$(Q)echo " CLEAN kernel "

.PHONY : distclean
distclean: clean

$(BINARY_DIR)/kernel_linker.lds: $(SOURCE_DIR)/kernel_linker.lds
	$(Q)echo " CPP     $@"
	${Q}${CC} -E -x c -P -Wp,-MD,$(BINARY_DIR)/kernel_linker.lds.dep,-MT,'kernel_linker.lds' ${INCLUDES} -o $@ $<

